/**
 * 
 */
package com.tw.trips.service;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tw.trips.domain.Location;
import com.tw.trips.domain.WeatherUpdate;
import com.tw.trips.util.HttpUtil;
import com.tw.trips.util.XMLUtil;

/**
 * @author M1005264
 *
 */
@Service
@Transactional
public class WeatherAlertService {

	@Autowired
	private LocationService locationService;

	@Autowired
	private HttpUtil httpUtil;

	private static final String weatherXML = "WeatherUpdate.xml";

	private static final String URL = "http://www.google.com/ig/api";

	private Map<String, Map<String,WeatherUpdate>> weatherMap = new HashMap<String, Map<String,WeatherUpdate>>();

//	@Async
	public void getAlerts(){
		Collection<Location> locations = locationService.getAllLocations();

		for(Location location : locations){
			getAlerts(location.getName());
		}
	}

	public Map<String, WeatherUpdate> getAlerts(String location){
		if(weatherMap.get(location) == null){
			String reqParams = "weather=" + location;
			//			InputStream is = this.getClass().getClassLoader().getResourceAsStream("WeatherUpdate.xml");
			try {
				//				String xmlData = XMLUtil.convertStreamToString(is);
				String xmlData = httpUtil.sendGetRequest(URL, reqParams);
				if(xmlData != null){
					XMLUtil parser = new XMLUtil(xmlData);
					List<String> dayOfWeeK = parser.getAttrFromAllTags("day_of_week", "data");
					List<String> low = parser.getAttrFromAllTags("low", "data");
					List<String> high = parser.getAttrFromAllTags("high", "data");
					List<String> icon = parser.getAttrFromAllTags("icon", "data");
					List<String> condition = parser.getAttrFromAllTags("condition", "data");

					Map<String,WeatherUpdate> updates = getWeatherUpdates(dayOfWeeK,low,high,icon,condition);
					weatherMap.put(location, updates);
					
					return updates;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(weatherMap.get(location) == null){

			InputStream weatherIS = this.getClass().getClassLoader().getResourceAsStream(weatherXML);
			String weather = "";
			try {
				weather = XMLUtil.convertStreamToString(weatherIS);
				XMLUtil parser = new XMLUtil(weather);
				List<String> dayOfWeeK = parser.getAttrFromAllTags("day_of_week", "data");
				List<String> low = parser.getAttrFromAllTags("low", "data");
				List<String> high = parser.getAttrFromAllTags("high", "data");
				List<String> icon = parser.getAttrFromAllTags("icon", "data");
				List<String> condition = parser.getAttrFromAllTags("condition", "data");

				Map<String,WeatherUpdate> updates = getWeatherUpdates(dayOfWeeK,low,high,icon,condition);
				weatherMap.put(location, updates);
				System.out.println(updates);
				return updates;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return weatherMap.get(location);
	}

	private Map<String, WeatherUpdate> getWeatherUpdates(
			List<String> dayOfWeeK, List<String> low, List<String> high,
			List<String> icon, List<String> condition) {
		Map<String, WeatherUpdate> updates = new HashMap<String, WeatherUpdate>();
		for(int i = 0 ; i <= 2 ; i ++){
			WeatherUpdate update = new WeatherUpdate();
			update.setCondition(condition.get(i+1));
			update.setDayOfWeek(dayOfWeeK.get(i));
			update.setHighData(high.get(i));
			update.setIcon(URL.substring(0, URL.length()-7) + icon.get(i+1));
			update.setLowData(low.get(i));

			updates.put(""+i, update);
		}

		return updates;
	}
}
